"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 主设备pg_log目录权限修改重启数据库
Create At   : 2020/07/23
Owner       : @li-xin12345
Description :
    1. 删除pg_log权限
    2. 重启数据库
    3. 恢复pg_log权限
    4.重启数据库
Expect      :
    1. 删除pg_log权限成功
    2. 重启数据库失败
    3. 无
    4.数据库可以正常启动
History     :
    modified: 2020/12/10  modified by @wan005,生成corefile不作为检查点
"""

import os
import sys
import unittest
import time
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class IndexFileDamaged(unittest.TestCase):
    logger.info('------------Opengauss_Reliability_Dbsys_Case186.py start------------')
    primaryRoot = Node('PrimaryRoot')
    primaryNode = Node(node='PrimaryDbUser')
    DB_ENV_PATH = macro.DB_ENV_PATH
    DB_LOG_PATH = macro.PG_LOG_PATH
    Constant = Constant()
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def test_Index_file_damaged(self):

        # 更改pg_log文件权限
        logger.info('----------------------------更改pg_log文件权限-----------------------------')
        excute_cmd = f'chmod -R 000 {self.DB_LOG_PATH};'
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.NO_FILE_MSG, msg)

        logger.info('-------------------重启数据库-------------------------------')
        time.sleep(5)
        logger.info('查询数据库状态')
        status = self.sh_primy.getDbClusterStatus("status")
        logger.info(status)
        result = self.sh_primy.stopDbCluster()
        self.assertTrue(result)
        result = self.sh_primy.startDbCluster()
        self.assertFalse(result)

        # 恢复pg_log文件权限
        logger.info('----------------------------恢复pg_log文件权限-----------------------------')
        excute_cmd = f'chmod -R 700 {self.DB_LOG_PATH};'
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.NO_FILE_MSG, msg)

        logger.info('-------------------重启数据库-------------------------------')
        time.sleep(5)
        logger.info('查询数据库状态')
        status = self.sh_primy.getDbClusterStatus("status")
        logger.info(status)
        result = self.sh_primy.stopDbCluster()
        self.assertTrue(result)
        result = self.sh_primy.startDbCluster()
        self.assertTrue(result)

    def tearDown(self):
        logger.info('----------------------------后置处理-----------------------------')
        excute_cmd = f'chmod -R 700 {self.DB_LOG_PATH};'
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.NO_FILE_MSG, msg)

        logger.info('-------------------重启数据库-------------------------------')
        time.sleep(5)
        logger.info('查询数据库状态')
        status = self.sh_primy.getDbClusterStatus("status")
        logger.info(status)
        result = self.sh_primy.stopDbCluster()
        self.assertTrue(result)
        result = self.sh_primy.startDbCluster()
        self.assertTrue(result)
        logger.info('----------------------------Reliability_Dbsys_Case186执行完成-----------------------------')
